#!/bin/sh

## Neovim
### Prepare
verify=`uname -a | grep arch`
if [ -v $verify ]; then
	packageM=0
else
	packageM=1
fi

echo "Avaliable Options: "
echo "0: Fresh install (default)."
echo "1: Pre-install, install dependences."
echo "2: Install neovim only."
echo "3: Post install, install support packages."
echo "4: Uninstall"
read -p "Please select what to install: " InstallMode

if [ -z $InstallMode ]; then
  $InstallMode=0
fi

if [ $InstallMode -eq 0 ]||[ $InstallMode -eq 1 ]; then

# if [ $packageM -eq 0 ]; then
	echo "安装依赖"
	sudo apt-get update
	sudo apt-get install ninja-build gettext libtool libtool-bin autoconf automake cmake g++ pkg-config unzip curl doxygen
	sudo apt-get install -y python-dev python3-dev python3-pip
	echo "删除vim"
	sudo apt-get remove -y vim-common vim-runtime vim-tiny vim

	echo "安装Nodejs"
	curl -sL https://deb.nodesource.com/setup_lts.x | sudo -E bash -
	sudo apt-get install -y nodejs
	
	echo "安装Yarn"
	curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -
	echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
	sudo apt-get update
	sudo apt remove yarn cmdtest
	sudo apt-get install -y yarn
# else
# 	sudo pacman -Syyu
# 	echo "删除vim"
# 	sudo pacman -R vim-common vim-runtime vim-tiny -vim
# 	echo "安装依赖"
# 	sudo pacman -S nodejs
# fi

echo "Nodejs 换国内源"
npm config set registry https://registry.npm.taobao.org/
echo "yarn 换国内源"
yarn config set registry https://registry.npm.taobao.org/

echo "安装remark, frontmatter for markdown"
sudo npm -g install -y remark remark-cli remark-stringify remark-frontmatter wcwidth

# echo "mdctags命令，markdown插件用"
# git clone https://github.com/wsdjeg/mdctags.rs.git
# cd ./mdctags.rs
# sudo apt install cargo
# cargo build --release

fi

if [ $InstallMode -eq 0 ]||[ $InstallMode -eq 2 ]; then

echo "开始安装Neovim"
echo "请选择安装方式："
echo "1: 通过APT包管理器"
echo "2: 通过构建源码"
read -p "请输入：" InstallFrom
if [ $InstallFrom -eq 2 ]; then
  sudo add-apt-repository ppa:neovim-ppa/stable
  sudo apt-get install -y neovim
else
  echo "安装构建所需依赖"
  sudo apt-get install ninja-build gettext libtool libtool-bin autoconf automake cmake g++ pkg-config unzip curl doxygen
  mkdir -p ~/Downloads/GitDownloads
  echo "克隆仓库"
  git clone https://github.com/neovim/neovim.git ~/Downloads/GitDownloads
  echo "选择稳定版本"
  cd ~/Downloads/GitDownloads/neovim
  git checkout stable
  echo "开始编译"
  make
  echo "开始安装"
  sudo make install

fi

fi

if [ $InstallMode -eq 0 ]||[ $InstallMode -eq 3 ]; then

echo "安装语言相关"
# bear: for ccle, generate compilation database
sudo apt install -y gcc clang clangd ccls bear fd-find

ln -s $(which fdfind) ~/.local/bin/fd

echo "添加Python支持"
pip3 install --user pynvim neovim
echo "添加Nodejs支持"
sudo npm install -g neovim
echo "添加yarn支持"
yarn global add neovim

# curl -sLf https://spacevim.org/cn/install.sh | bash
echo "安装Spacevim"
curl -sLf https://spacevim.org/cn/install.sh | bash -s -- --install neovim 
# curl -sLf https://spacevim.org/cn/install.sh | bash -s -- --uninstall

fi

if [ $InstallMode -eq 4 ]; then
  echo "卸载中..."
  sudo rm /usr/local/bin/nvim
  sudo rm -r /usr/local/share/nvim
  echo "已卸载"
fi
